<?php

if (!defined('IN_XLP')) {
	exit('Access Denied!');
}

/**
 * Description of OrderModel
 *
 * @author skyinter
 */
class expressModel extends Model {
	
	function __construct() {
		parent::__construct();
		$this->dbTable = 'express';
		$this->page = 20;
	}

	/*
	 * 用户是否在商品配送范围内
	 */
	public function inScope($goods_id) {

        $scope = $this->goodsScope($goods_id);
        //没有设定范围
        if(empty($scope)) return true;

        $vid = user::$userInfo['village_id'];
		if(!$vid) {
			sendJson(array('err'=>1, 'msg'=>'您还没有绑定任何小区'));
		}
		return in_array($vid, $scope) == true;
	}

	/*
	 * 所有配送小区
	 */
	public function scope() {
		$result = M('village')->field('`vid`,`title`')->where(array('is_open'=>1))->findAll();
		return $result;
	}

	/*
	 * 商品配送小区范围
	 */
	public function goodsScope($goods_id) {
		$result = M('goods')->field('`goods_scope`')->where(array('id'=>$goods_id))->find();
		return unserialize($result['goods_scope']);
	}

	/*
	 * 商品配送小区范围 + 其余范围
	 */
	public function goodsCheckScope($goods_id) {
		$all_scope = $this->scope();
		$checked_scope = $this->goodsScope($goods_id);
		foreach ($all_scope as $k => $v) {
			if(@in_array($v['vid'], $checked_scope)) {
				$all_scope[$k]['checked'] = 1;
			} else {
				$all_scope[$k]['checked'] = 0;
			}
		}
        return $all_scope;
	}

}